package com.xbongbong.parent.help;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.dto.ParseSingleRowDataDTO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.parent.parse.data.FormDataDetailAnalysisDataHelp;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.MarketActivityEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.enums.business.SupplierContactEnum;
import com.xbongbong.saas.enums.business.SupplierEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class PushMessageHelp {

    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private FormDataDetailAnalysisDataHelp formDataDetailAnalysisDataHelp;

    public String getMessageContent (PaasProcessDataEntity paasProcessDataEntity, Map<String, UserEntity> userMap, Map<String, DepartmentEntity> departmentMap, BaseDTO baseDTO) throws XbbException {
        String content = "";
        Integer saasMark = paasProcessDataEntity.getSaasMark();
        JSONObject jsonData = JSON.parseObject(paasProcessDataEntity.getData());
        if (Objects.equals(saasMark, SaasMarkEnum.SAAS.getCode())) {
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(paasProcessDataEntity.getBusinessType());
            switch (xbbRefTypeEnum) {
                case CUSTOMER_MANAGEMENT:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, CustomerManagementEnum.NAME.getAttr(), "");
                    break;
                case SALES_OPPORTUNITY:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, SalesOpportunityEnum.NAME.getAttr(), "");
                    break;
                case CONTRACT:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, ContractEnum.NAME.getAttr(), "");
                    break;
                case REFUND:
                case RETURN:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, RefundEnum.REFUND_NAME.getAttr(), "");
                    break;
                case PURCHASE:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, PurchaseEnum.PURCHASE_NAME.getAttr(), "");
                    break;
                case QUOTATION:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, QuotationEnum.NAME.getAttr(), "");
                    break;
                case SUPPLIER:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, SupplierEnum.SUPPLIER_NAME.getAttr(), "");
                    break;
                case SUPPLIER_CONTACT:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, SupplierContactEnum.NAME.getAttr(),"");
                    break;
                case CONTACT:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, ContactEnum.NAME.getAttr(),"");
                    break;
                case CLUE:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, ClueEnum.COMPANY_NAME.getAttr(),"");
                    break;
                case MARKET_ACTIVITY:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, MarketActivityEnum.NAME.getAttr(),"");
                    break;
                case COMPETITOR:
                    content = FastJsonHelper.getStringOrDefaultFromFormData(jsonData, CompetitorEnum.NAME.getAttr(),"");
                    break;
                case RETURNED_PURCHASE:
                case INSTOCK:
                case PURCHASE_INSTOCK:
                case REFUND_INSTOCK:
                case OTHER_INSTOCK:
                case PRODUCTION_INSTOCK:
                case RETURNED_MATERIEL_INSTOCK:
                case OUTSTOCK:
                case CONTRACT_OUTSTOCK:
                case RETURNED_PURCHASE_OUTSTOCK:
                case OTHER_OUTSTOCK:
                case PRODUCTION_MATERIEL_OUTSTOCK:
                case WORK_ORDER_OUTSTOCK:
                case ASSEMBLE:
                case TRANSFER:
                case INVENTORY:
                case INVOICE:
                case PURCHASE_INVOICE:
                case PRODUCTION_ORDER:
                case WORK_ORDER:
                case BOM_BILL:
                case PAY_SHEET:
                case PAYMENT_SHEET:
                case CREDIT_TEMPORARY_LIMIT:
                case CREDIT_FIXED_LIMIT:
                case OTHER_INCOME:
                case OTHER_EXPENSE:
                case FUND_TRANSFER:
                case RECEIPT_ORDER:
                case WORK_ORDER_V2:
                case SERVICE_PROJECT:
                case COST_ADJUST:
                case ORDER:
                    content = paasProcessDataEntity.getSerialNo();
                    break;
                default:
                    break;
            }
        } else {
            // 获取标题
            PaasFormDataEntityExt paasFormDataEntity = new PaasFormDataEntityExt();
            BeanUtil.copyProperties(paasProcessDataEntity, paasFormDataEntity);
            paasFormDataEntity.setData(jsonData);
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(paasProcessDataEntity.getFormId(), paasProcessDataEntity.getCorpid());
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(paasProcessDataEntity.getFormId(), paasProcessDataEntity.getCorpid());
            List<FieldAttrEntity> explainList = JSONArray.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
            Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(paasFormExplainEntity.getExplains(), null);

            //  获取用户和部门容器 解析paas的特殊数据 end
            ParseSingleRowDataDTO parseSingleRowDataDTO = new ParseSingleRowDataDTO();
            BeanUtil.copyProperties(baseDTO, parseSingleRowDataDTO);
            BeanUtil.copyProperties(paasProcessDataEntity, parseSingleRowDataDTO);
            parseSingleRowDataDTO.setDataJsonObject(jsonData);
            parseSingleRowDataDTO.setExplainMap(explainMap);
            parseSingleRowDataDTO.setUserMap(userMap);
            parseSingleRowDataDTO.setDepartmentMap(departmentMap);
            // 解析数据
            ParseSingleRowDataVO parseSingleRowDataVO = formDataDetailAnalysisDataHelp.parseSingleRowData(parseSingleRowDataDTO);
            if (Objects.isNull(parseSingleRowDataVO)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PARSE_SINGLE_ROW_DATA_FAILED);
            }
            jsonData = parseSingleRowDataVO.getResult();

            UserEntity user = userMap.get(paasProcessDataEntity.getOwnerId());
            ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
            if (Objects.nonNull(user)) {
                BeanUtil.copyProperties(user, returnUserAndDepartmentPojo);
                returnUserAndDepartmentPojo.setId(user.getUserId());
            }
            jsonData.put(FieldTypeEnum.OWNERID.getAlias(), Collections.singletonList(returnUserAndDepartmentPojo));

            List<FormTitlePoJo> formTitleList = ExplainUtil.getFormTitle(JSONObject.parseArray(paasFormEntityExt.getCustomTitle()), explainMap, jsonData, explainList, paasFormDataEntity);
            StringBuilder stringBuilder = new StringBuilder();
            for (FormTitlePoJo item : formTitleList) {
                if (Objects.nonNull(item.getValue())) {
                    for (Object value : item.getValue()) {
                        if (Objects.nonNull(value) && StringUtil.isNotEmpty(value.toString())) {
                            stringBuilder.append(Objects.toString(value, "")).append(StringConstant.COMMA);
                        }
                    }
                }
            }
            if (stringBuilder.length() > 0) {
                content = stringBuilder.substring(0, stringBuilder.length() - 1);
            }
        }
        return content;
    }
}
